package com.aizhixin.lab.project.template.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.project.template.domain.ProjectCourseDomain;
import com.aizhixin.lab.project.template.domain.ProjectDomain;
import com.aizhixin.lab.project.template.entity.Project;
import com.aizhixin.lab.project.template.entity.ProjectCourse;
import com.aizhixin.lab.project.template.repository.ProjectCourseRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class ProjectCourseService {
    @Autowired
    private ProjectCourseRepository courseRepository;
    public List<ProjectCourse> save(List<ProjectCourse> courseList){
       return courseRepository.save(courseList);
    }
    public ProjectCourse save(ProjectCourse projectCourse){
        return courseRepository.save(projectCourse);
    }
    public Map<String, Object> delete(String id) {
        Map<String, Object> result = new HashMap<String, Object>();
        ProjectCourse projectCourse = courseRepository.findOne(id);
        if (!Objects.isNull(projectCourse)) {
            projectCourse.setDeleteFlag(DataValidity.INVALID.getState());
            save(projectCourse);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, "删除成功");
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "未找到课程信息");
        }
        return result;
    }
    public Map<String, Object> updateCourse(ProjectCourseDomain domain) {
        Map<String, Object> result = new HashMap<String, Object>();
        ProjectCourse projectCourse = courseRepository.findOne(domain.getId());
        if (Objects.isNull(projectCourse)) {
            BeanUtils.copyProperties(domain, projectCourse);
            save(projectCourse);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA, "修改成功");
        } else {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "项目信息不存在");
        }
        return result;
    }
    public ProjectCourse save(ProjectCourseDomain domain) {
        ProjectCourse projectCourse = new ProjectCourse();
        BeanUtils.copyProperties(domain, projectCourse);
        projectCourse.setId(UUID.randomUUID().toString());
        projectCourse.setDeleteFlag(DataValidity.VALID.getState());
        return save(projectCourse);
    }
    public Integer deleteByProjectId(String id){
       return courseRepository.deleteByProjectId(id);
    }
    public List<ProjectCourse> projectCourseListByProjectId(String projectId){
        return courseRepository.findByProjectId(projectId);
    }
}
